package com.yw.android.common.base;


import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yw.android.common.utils.TUtil;


public abstract class BaseActivity<T extends BasePresenter, E extends BaseModel> extends Activity {
    public T mPresenter;
    public E mModel;
    public Context mContext;
    private ProgressDialog dialog;


    @Override
    public void onDestroy() {
        mPresenter.onStop();
        super.onDestroy();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTimeTrans();
        setContentView(getLayoutId());
        mContext = this;
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        if (mPresenter != null) {
            mPresenter.mContext = this;
        }
        this.initPresenter();
        this.initView();
    }

    /********************************
     ********* 子类实现 ************
     *****************************/
    //获取布局文件
    public abstract int getLayoutId();

    //简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
    public abstract void initPresenter();

    //初始化view
    public abstract void initView();

    public void showProgress(int titleId, int contentId) {
        if (dialog == null) {
            dialog = new ProgressDialog(this);
            dialog.setTitle(titleId);
            dialog.setMessage(getString(contentId));
        }

        if (!dialog.isShowing()) dialog.show();
    }

    public void dismissProgress() {
        if (dialog != null && dialog.isShowing()) dialog.dismiss();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    protected void setTimeTrans() {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | 0x00002000);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0xffffffff);
            if (Build.VERSION.SDK_INT >= 26) {
                window.setNavigationBarColor(0xffffffff); // 浅底
                setNavigationBarIconDark(true); // 灰键
            }
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    private void setNavigationBarIconDark(boolean isDark) {
        View decor = getWindow().getDecorView();
        int flag = decor.getSystemUiVisibility();
        if (isDark) {
            flag |= 0x00000010;
        } else {
            flag &= ~0x00000010;
        }
        decor.setSystemUiVisibility(flag);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res != null && res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
}
